<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :maskClosable="false">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入供应商编码" v-decorator="['supplierCode', validatorRules.supplierCode]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入供应商名称" v-decorator="['supplierName', validatorRules.supplierName]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="供应商类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['supplierType', validatorRules.supplierType]" :trigger-change="true" dictCode="supplier_type" placeholder="请选择供应商类型" />
            </a-form-item>
          </a-col>
          
          <!-- 联系人信息区域 -->
          <a-col :span="24">
            <a-divider orientation="left">
              联系人信息
              <a-tooltip>
                <template slot="title">
                  填写联系人信息后，系统将自动创建一条供应商联系人记录
                </template>
                <a-icon type="question-circle" style="margin-left: 8px; color: #1890ff;" />
              </a-tooltip>
            </a-divider>
          </a-col>
          <a-col :span="12">
            <a-form-item label="主要联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入主要联系人" v-decorator="['contactPerson']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入联系电话" v-decorator="['contactPhone']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="电子邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入电子邮箱" v-decorator="['email']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="职位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入职位" v-decorator="['position']" />
            </a-form-item>
          </a-col>
          
          <!-- 其他信息区域 -->
          <a-col :span="24">
            <a-divider orientation="left">其他信息</a-divider>
          </a-col>
          <a-col :span="12">
            <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入地址" v-decorator="['address']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="税号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入税号" v-decorator="['taxNumber']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开户银行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入开户银行" v-decorator="['bankName']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="银行账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入银行账号" v-decorator="['bankAccount']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="信用等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['creditLevel']" :trigger-change="true" dictCode="supplier_credit_level" placeholder="请选择信用等级" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合作状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['cooperationStatus', validatorRules.cooperationStatus]" :trigger-change="true" dictCode="supplier_cooperation_status" placeholder="请选择合作状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="{span: 3}" :wrapperCol="{span: 21}">
              <a-textarea placeholder="请输入备注" v-decorator="['remark']" :rows="4" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { supplierAdd, supplierEdit, supplierDetail } from '@/api/wms/supplier'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import pick from 'lodash.pick'
  
  export default {
    name: "SupplierModal",
    components: {
      JDictSelectTag
    },
    data () {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 18
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          supplierCode: {
            rules: [
              { required: true, message: '请输入供应商编码!' }
            ]
          },
          supplierName: {
            rules: [
              { required: true, message: '请输入供应商名称!' }
            ]
          },
          supplierType: {
            rules: [
              { required: true, message: '请选择供应商类型!' }
            ]
          },
          cooperationStatus: {
            rules: [
              { required: true, message: '请选择合作状态!' }
            ]
          }
        },
        url: {
          add: "/wms/supplier/add",
          edit: "/wms/supplier/edit"
        }
      }
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 
            'supplierCode', 
            'supplierName', 
            'supplierType', 
            'contactPerson', 
            'contactPhone', 
            'email', 
            'position',
            'address', 
            'taxNumber', 
            'bankName', 
            'bankAccount', 
            'creditLevel', 
            'cooperationStatus', 
            'remark'))
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const { form } = this
        form.validateFields((err, values) => {
          if (!err) {
            this.confirmLoading = true;
            let formData = Object.assign({}, values);
            
            // 处理联系人信息
            if (formData.contactPerson && formData.contactPhone) {
              // 创建联系人对象
              const contact = {
                name: formData.contactPerson,
                phone: formData.contactPhone,
                email: formData.email || '',
                position: formData.position || '',
                isPrimary: 1, // 设为主要联系人
                remark: ''
              };
              
              // 添加到contactList
              formData.contactList = [contact];
            }
            
            let obj;
            if (!this.model.id) {
              obj = supplierAdd(formData);
            } else {
              formData.id = this.model.id;
              obj = supplierEdit(formData);
            }
            
            obj.then((res) => {
              if (res.success) {
                this.$message.success(res.message);
                this.$emit('ok');
                this.close();
              } else {
                this.$message.warning(res.message);
              }
            }).finally(() => {
              this.confirmLoading = false;
            });
          }
        })
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>

<style scoped>
.ant-divider-inner-text {
  font-weight: bold;
  font-size: 14px;
}
</style> 